package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;
import dto.BookDTO;
import dto.LogonDTO;

public class ViewBookPage extends JDialog {

	private static final long serialVersionUID = 1L;

	private JTextField textFieldTitle;
	private JTextField textFieldAuthor;
	private JTextField textFieldPublisher;
	private JTextField textFieldPublishedDate;
	private JTextArea textAreaSynopsis;
	private JTextField textFieldISBN;
	private JTextField textFieldCatalog;
	private JTextField textFieldAvailability;
	private JTextField textFieldRemarks;
	private JTextField textFieldEditorUsername;
	private JTextField textFieldEditorLevel;

	private JButton btnBack = new JButton("Back");

	/**
	 * Create the dialog.
	 */
	public ViewBookPage(LogonDTO logonDTO, BookDTO book) {

		setResizable(false);
		setBounds(100, 100, 571, 468);
		getContentPane().setLayout(
				new MigLayout("", "[grow][170.00,grow][98.00][grow][]",
						"[][][][][][][60.00,grow][][][][][][][][][]"));

		JLabel lblJaLibrary = new JLabel("J&A Library");
		lblJaLibrary.setFont(new Font("Arial", Font.BOLD, 16));
		lblJaLibrary.setForeground(Color.BLUE);
		getContentPane().add(lblJaLibrary, "cell 0 0 5 1,alignx center");

		JLabel lblEditBook = new JLabel("View Book");
		getContentPane().add(lblEditBook, "cell 0 1 5 1,alignx center");

		JLabel label = new JLabel("     ");
		getContentPane().add(label, "cell 0 2");

		JLabel lblTitle = new JLabel("Title:");
		getContentPane().add(lblTitle, "cell 0 3,alignx trailing");

		textFieldTitle = new JTextField();
		textFieldTitle.setEditable(false);
		textFieldTitle.setText(book.getTitle());
		getContentPane().add(textFieldTitle, "cell 1 3 3 1,growx");
		textFieldTitle.setColumns(10);

		JLabel lblAuthor = new JLabel("Author:");
		getContentPane().add(lblAuthor, "cell 0 4,alignx trailing");

		textFieldAuthor = new JTextField();
		textFieldAuthor.setEditable(false);
		textFieldAuthor.setText(book.getAuthor());
		getContentPane().add(textFieldAuthor, "cell 1 4 3 1,growx");
		textFieldAuthor.setColumns(10);

		JLabel lblPublisher = new JLabel("Publisher:");
		getContentPane().add(lblPublisher, "cell 0 5,alignx trailing");

		textFieldPublisher = new JTextField();
		textFieldPublisher.setEditable(false);
		textFieldPublisher.setText(book.getPublisher());
		getContentPane().add(textFieldPublisher, "cell 1 5,growx");
		textFieldPublisher.setColumns(10);

		JLabel lblPublishedDate = new JLabel("Published Date:");
		getContentPane().add(lblPublishedDate, "cell 2 5,alignx trailing");

		textFieldPublishedDate = new JTextField();
		textFieldPublishedDate.setEditable(false);
		textFieldPublishedDate.setText(book.getPublishedDate());
		getContentPane().add(textFieldPublishedDate, "cell 3 5,growx");
		textFieldPublishedDate.setColumns(10);

		JLabel lblSynopsis = new JLabel("Synopsis:");
		getContentPane().add(lblSynopsis, "cell 0 6,alignx trailing");

		textAreaSynopsis = new JTextArea();
		textAreaSynopsis.setLineWrap(true);
		textAreaSynopsis.setText(book.getSynopsis());
		textAreaSynopsis.setEditable(false);
		getContentPane().add(textAreaSynopsis, "cell 1 6 3 4,grow");

		JLabel lblIsbn = new JLabel("ISBN:");
		getContentPane().add(lblIsbn, "cell 0 10,alignx trailing");

		textFieldISBN = new JTextField();
		textFieldISBN.setEditable(false);
		textFieldISBN.setText(book.getISBN());
		getContentPane().add(textFieldISBN, "cell 1 10 2 1,growx");
		textFieldISBN.setColumns(10);

		JLabel lblCatalogddc = new JLabel("Catalog (DDC):");
		getContentPane().add(lblCatalogddc, "cell 0 11,alignx trailing");

		textFieldCatalog = new JTextField();
		textFieldCatalog.setEditable(false);
		textFieldCatalog.setText(book.getDeweyDecimalNo());
		getContentPane().add(textFieldCatalog, "cell 1 11 2 1,growx");
		textFieldCatalog.setColumns(10);

		JLabel lblAvailability = new JLabel("Availability:");
		getContentPane().add(lblAvailability, "cell 0 12,alignx trailing");

		textFieldAvailability = new JTextField();
		textFieldAvailability.setEditable(false);

		if ("N".equalsIgnoreCase(book.getOnLoan()))
			textFieldAvailability.setText("Available");
		else if ("Y".equalsIgnoreCase(book.getOnLoan()))
			textFieldAvailability.setText("Not Available");

		getContentPane().add(textFieldAvailability, "cell 1 12 2 1,growx");
		textFieldAvailability.setColumns(10);

		// REMARKS can only be seen by librarian or admin
		if (logonDTO.getLevel() == 1 || logonDTO.getLevel() == 2) {
			JLabel lblRemarks = new JLabel("Remarks:");
			getContentPane().add(lblRemarks, "cell 0 13,alignx trailing");

			textFieldRemarks = new JTextField();
			textFieldRemarks.setEditable(false);
			textFieldRemarks.setText(book.getRemarks());
			getContentPane().add(textFieldRemarks, "cell 1 13 3 1,growx");
			textFieldRemarks.setColumns(10);
		}

		JLabel label_1 = new JLabel("     ");
		getContentPane().add(label_1, "cell 0 13");

		// BACK TO SEARCH button
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		getContentPane().add(btnBack, "cell 4 14");

		textFieldEditorUsername = new JTextField();
		textFieldEditorUsername.setEditable(false);
		textFieldEditorUsername.setText(logonDTO.getUserName());
		getContentPane().add(textFieldEditorUsername, "cell 0 15 2 1,growx");
		textFieldEditorUsername.setColumns(10);

		textFieldEditorLevel = new JTextField();
		textFieldEditorLevel.setEditable(false);
		textFieldEditorLevel.setText(logonDTO.getUserTitle());
		getContentPane().add(textFieldEditorLevel, "cell 3 15 2 1,growx");
		textFieldEditorLevel.setColumns(10);
	}
}
